﻿import mx.utils.Delegate;
class cn.eminia.AS2.MoE {
	// [置顶]
	// cn.eminia.AS2.MoE.follow(this)
	function MoE() {
	}
	static function follow(a:MovieClip, obj:Object):Void {
		var d:String = obj.d;
		var limit:Array = obj.limit;
		var inside:Function = obj.inside;
		var outside:Function = obj.outside;
		//
		if (limit != null && limit.length != 2) {
			trace('驳回!! 请为 limit 正型');
			return;
		}
		if (d == null) {
			a.onEnterFrame = function() {
				this._x += (this._parent._xmouse-this._x)*.3;
				this._y += (this._parent._ymouse-this._y)*.3;
			};
			return;
		}
		a.d = d;
		if (limit == null) {
			a.onEnterFrame = function() {
				this[d] += (this._parent[d+'mouse']-this[d])*.3;
			};
			return;
		}
		// USAGE       
		//		cn.eminia.AS2.EffoM.follow(girl_mc,{d:'_x', limit:[50, 404], inside:function () {
		//Mouse.hide();
		//}, outside:function () {
		//Mouse.show();
		//}});
		a.el = limit;
		a.ei = inside;
		a.eo = outside;
		a.es = null;
		a.onEnterFrame = function() {
			// KEEP FOR EVER !! 
			// var m = Math.max(el[0], Math.min(el[1], this._parent[d+'mouse']));
			var m = this._parent[d+'mouse'];
			var el = this.el;
			var ei = this.ei;
			var eo = this.eo;
			var es = this.es;
			this[d] += (Math.max(el[0], Math.min(el[1], this._parent[d+'mouse']))-this[d])*.3;
			if ((m<el[0] || m>el[1]) && (es == null || es == 'in')) {
				es = 'out';
				eo();
			} else if (es == null || es == 'out') {
				es = 'in';
				ei();
			}
		};
		// **********************************
		// KEEP FOR EVER !! 
		// VERSION 1
		/*else if (d == 'x') {
		if (limit == null) {
		a.onEnterFrame = function() {
		this._x += (this._parent._xmouse-this._x)*.3;
		};
		} else {
		a.limit = limit;
		a.onEnterFrame = function() {
		var m = Math.max(limit[0], this._parent._xmouse);
		m = Math.min(limit[1], m);
		this._x += (m-this._x)*.3;
		};
		}
		} else if (d == 'y') {
		if (limit == null) {
		a.onEnterFrame = function() {
		this._y += (this._parent._ymouse-this._y)*.3;
		};
		} else {
		a.limit = limit;
		a.onEnterFrame = function() {
		var m = Math.max(limit[0], this._parent._ymouse);
		m = Math.min(limit[1], m);
		this._y += (m-this._y)*.3;
		};
		}
		}*/
	}
	static function isClick(a:MovieClip, obj:Object):Void {
		if (obj == null) {
			var obj = {};
		}
		obj.onMouseDown = Delegate.create(obj, function () {
			if (a.hitTest(_root._xmouse, _root._ymouse)) {
				//trace('click');
				this.callback({it:a,id:cn.eminia.AS2.BoGV3.getLastID(a._name)});
			}
		});
		Mouse.addListener(obj);
	}
	static function over(a:MovieClip, obj:Object):Void {
		if (obj == null) {
			var obj = {};
		}
		obj.a = a;
		obj.m = 0;
		obj.auto = (obj.auto == undefined) ? true : obj.auto;
		if (obj.scope != null) {
			var scope = obj.scope;
			obj.inside = Delegate.create(scope, obj.inside);
			obj.outside = Delegate.create(scope, obj.outside);
		}
		// KEEP FOR EVER !!        
		//a.onEnterFrame = Delegate.create(obj, function () {
		obj.onMouseMove = Delegate.create(obj, function () {
			if (a.hitTest(_root._xmouse, _root._ymouse) && (this.m == 0)) {
				if (this.auto) {
					Mouse.hide();
				}
				this.inside({it:a});
				this.m = 1;
			} else if (!a.hitTest(_root._xmouse, _root._ymouse) && (this.m == 1)) {
				if (this.auto) {
					Mouse.show();
				}
				this.outside({it:a});
				this.m = 0;
			}
		});
		Mouse.addListener(obj);
		//a.EffoM_=obj;
	}
	static function equal(a:MovieClip):Void {
		var obj = {};
		obj.onMouseMove = Delegate.create(a, function () {
			a._x = a._parent._x;
			a._y = a._parent._y;
		});
	}
	static function del(a) {
		delete a.onEnterFrame;
	}
}
